package tc.jv.task12;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Reflexio
{

	public static void main(String[] args)
	{
		String className  = args[0],
			   methodName = args[1],
			   arg        = args[2];
		
		invokeMethod(className, methodName, arg);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Double invokeMethod(String className, String methodName, String arg)
	{
		System.out.println("Input args: ");
		System.out.printf("className: %s\n", className);
		System.out.printf("methodName: %s\n", methodName);
		System.out.printf("arg: %s\n", arg);
		System.out.println();
		
		try
		{
			Class c = Class.forName(className);
			System.out.printf("found: %s\n", c.toString());
			
			Method m = c.getMethod(methodName, Double.TYPE);
			System.out.printf("found: %s\n", m.toString());
			
			Double result = (Double) m.invoke(null, Double.parseDouble(arg));
			System.out.printf("result: %s\n", result);
			System.out.println("-------------------------");
			
			return result;
		}
		catch (ClassNotFoundException e)
		{
			System.err.println("Class not found!");
		}
		catch (NoSuchMethodException e)
		{
			System.err.println("Method not found!");
		}
		catch (SecurityException e)
		{
			e.printStackTrace();
		}
		catch (NumberFormatException e)
		{
			System.err.println("Invalid number format");
		}
		catch (IllegalAccessException e)
		{
			e.printStackTrace();
		}
		catch (IllegalArgumentException e)
		{
			e.printStackTrace();
		}
		catch (InvocationTargetException e)
		{
			e.printStackTrace();
		}
		
		return null;
	}

}
